/*
* _08LumaQQ - Java _08QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.qq;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import edu.tsinghua.lumaqq.qq.net.IConnection;
import edu.tsinghua.lumaqq.qq.packets.ErrorPacket;
import edu.tsinghua.lumaqq.qq.packets._08InPacket;
import edu.tsinghua.lumaqq.qq.packets._08OutPacket;

public class ResendTrigger<T> implements Callable<T> {
    private _08QQClient client;

    // 超时队列
    private Queue<_08OutPacket> timeoutQueue;
    // 包 to Port的映射
    private Map<_08OutPacket, String> toPort;

    // temp variable
    private String portName;

    public ResendTrigger(_08QQClient client) {
        this.client = client;
        timeoutQueue = new LinkedList<_08OutPacket>();
        toPort = new HashMap<_08OutPacket, String>();
        _08QQClient.executor.schedule(this, _08QQ.QQ_TIMEOUT_SEND, TimeUnit.MILLISECONDS);
    }

    /**
     * 添加一个包到超时队列
     *
     * @param packet 发送包对象
     * @param name   port名称
     */
    public synchronized void add(_08OutPacket packet, String name) {
        timeoutQueue.offer(packet);
        toPort.put(packet, name);
    }

    /**
     * 清空重发队列
     */
    public synchronized void clear() {
        timeoutQueue.clear();
        toPort.clear();
    }

    /**
     * 得到超时队列的第一个包，不把它从队列中删除
     *
     * @return 超时队列的第一个包，如果没有，返回null
     */
    public synchronized _08OutPacket get() {
        return timeoutQueue.peek();
    }

    /**
     * 得到超时队列的第一个包，并把它从队列中删除
     *
     * @return 超时队列的第一个包，如果没有，返回null
     */
    public synchronized _08OutPacket remove() {
        _08OutPacket ret = timeoutQueue.poll();
        if (ret != null)
            portName = toPort.remove(ret);
        return ret;
    }

    /**
     * 删除ack对应的请求包
     *
     * @param ack
     */
    public synchronized void remove(_08InPacket ack) {
        for (Iterator<_08OutPacket> i = timeoutQueue.iterator(); i.hasNext();) {
            _08OutPacket packet = i.next();
            if (ack.equals(packet)) {
                toPort.remove(packet);
                i.remove();
                break;
            }
        }
    }

    /**
     * 得到下一个包的超时时间
     *
     * @return 下一个包的超时时间，如果队列为空，返回一个固定值
     */
    private long getTimeoutLeft() {
        _08OutPacket packet = get();
        if (packet == null)
            return _08QQ.QQ_TIMEOUT_SEND;
        else
            return packet.getTimeout() - System.currentTimeMillis();
    }

    /**
     * 触发超时事件
     *
     * @param p
     */
    private void fireOperationTimeOutEvent(_08OutPacket packet, String portName) {
        ErrorPacket error = new ErrorPacket(ErrorPacket.ERROR_TIMEOUT, client.getUser());
        error.timeoutPacket = packet;
        error.setHeader(packet.getHeader());
        error.setFamily(packet.getFamily());
        client.addIncomingPacket(error, portName);
    }

    /* (non-Javadoc)
      * @see java.util.concurrent.Callable#call()
      */
    public T call() throws Exception {
        long t = getTimeoutLeft();
        while (t <= 0) {
            _08OutPacket packet = remove();

            // 发送
            IConnection port = client.getConnection(portName);
            if (port != null && packet != null && !port.getPolicy().isReplied(packet, false)) {
                if (packet.needResend()) {
                    // 重发次数未到最大，重发
                    client.sendPacketAnyway(packet, portName);
                } else {
                    // 触发操作超时事件
                    fireOperationTimeOutEvent(packet, portName);
                }
            }
            t = getTimeoutLeft();
        }
        _08QQClient.executor.schedule(this, t, TimeUnit.MILLISECONDS);

        return null;
    }
}
